const chalk = require("chalk");
const downloadUtil = require("download-git-repo");
const ora = require("ora-classic");

const download = (url, dest) => {
  return new Promise((resolve, reject) => {
    const spinner = ora(chalk.yellow("正在下载模版...\n")).start();
    spinner.color = "yellow";
    downloadUtil(`direct:${url}`, dest, { clone: true }, (err) => {
      if (err) {
        spinner.fail(chalk.red("模版下载失败！", err.statusMessage));
        spinner.fail();
        reject();
      } else {
        spinner.succeed("模版下载成功！");
        spinner.stop();
        resolve();
      }
      spinner.stop();
    });
  });
};

module.exports = download;
